﻿
function previewImage(fileInputId, previewImageId, checkInputId)
{
    var previewImage = document.getElementById(previewImageId);
    var fileInput = document.getElementById(fileInputId);
    var checkInput = document.getElementById(checkInputId);
    alert(fileInput.src);
    previewImage.src = fileInput.value;
    checkInput.value = "1";
}

function createUploadify(divId, userId, buttonText, fileSizeLimit, fileQuantityLimit, fileSizeLimitMessage)
{
    var div = $("#"+divId);
    var input = div.find("input[type=file]");
    input.uploadify({
        'swf'             : '/js/uploadify.swf',
        'preventCaching'  : true,
        'buttonText'      : buttonText,
        'fileSizeLimit'   : fileSizeLimit,
        'uploader'        : '/UploadFile.aspx',
        'method'          : 'post',
        'multi'           : false,
        'width'           : 80,
        'height'          : 27,
        'formData'        : { 'category' : 'none', 'userId' : userId },
        'onUploadSuccess' : function(file, data, response) {
            var obj = jQuery.parseJSON(data);
            if (obj.result)
            {
                addAttachment(div, "", obj.dfile, file.name);
            }
        }
    });
}

function addAttachment(div, id, generatedName, fileName)
{
    var downloadLink = "<div class='attachment'>";
    downloadLink += "<input type=\"hidden\" name=\"attachmentId\" value=\""+id+"\"/>";
    downloadLink += "<a href=\"/DownloadFile.aspx?dfile=";
    downloadLink += generatedName
    downloadLink += "\" target=\"_blank\">"+fileName+"</a>";
    downloadLink += "<img class=\"remove\" src=\"/image/layout/RemoveIcon.png\" onclick=\"removeAttachment(this);\"></img>&nbsp;&nbsp;";
    downloadLink += "</div>";
    div.append(downloadLink);
}

function removeAllAttachments(div)
{
    div.find(".attachment").remove();
}

function removeAttachment(button)
{
    var file = $(button).closest(".attachment");
    file.remove();
}

function ensureFileQuantityLimit(div, maxQuantity)
{
    var files = div.find(".attachment");
    if (files.length >= maxQuantity)
    {
        files.remove();
    }
}